package net.expectx.gw.service.impl;


import net.expectx.common.base.*;
import net.expectx.common.db.DataSourceEnum;
import net.expectx.common.db.DynamicDataSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lijian
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {
	@Autowired
	private BaseMapper<T> baseMapper;

	@Override
	public BaseResult insertSelective(T record) {
		DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
		Map<String, Object> map = new HashMap<String, Object>(2);
		String msg = "";
		boolean flag = false;
		try {
			flag = baseMapper.insertSelective(record) > 0 ? true : false;
			if (!flag) {
				msg = "保存失败";
			}
			map.put(BaseConstants.BASIC_IDENTIFICATION_RECORD,record);
		} catch (Exception e) {
			e.printStackTrace();
			msg = e.getLocalizedMessage();
		}
		if (!flag){
            map.put(BaseConstants.BASIC_IDENTIFICATION_CODE, BaseResultConstants.EXCEPTION_DATA_BASE_INSERT);
        }
		map.put(BaseConstants.BASIC_IDENTIFICATION_SUCCESS, flag);
		map.put(BaseConstants.BASIC_IDENTIFICATION_MESSAGE, msg);
		DynamicDataSource.clearDataSource();
		 return new BaseResult(flag,msg,record);
	}

	@Override
	public int deleteByPrimaryKey(Object id) {
		DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
		int i=baseMapper.deleteByPrimaryKey(Long.parseLong(id.toString()));
		DynamicDataSource.clearDataSource();
		return i;
	}
	@Override
	 public int deleteByStrWhere(String strWhere){
		DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
		int i=baseMapper.deleteByStrWhere(strWhere);
		DynamicDataSource.clearDataSource();
		return i;
	}
	@Override
	public BaseResult updateByPrimaryKeySelective(T record) {
		DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
		Map<String, Object> map = new HashMap<String, Object>(2);
		String msg = "";
		boolean flag = false;
		try {
			flag = baseMapper.updateByPrimaryKeySelective(record) > 0 ? true : false;
			if (!flag) {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = e.getLocalizedMessage();
		}
        if (!flag){
            map.put(BaseConstants.BASIC_IDENTIFICATION_CODE, BaseResultConstants.EXCEPTION_DATA_BASE_UPDATE);
        }
		map.put(BaseConstants.BASIC_IDENTIFICATION_SUCCESS, flag);
		map.put(BaseConstants.BASIC_IDENTIFICATION_MESSAGE, msg);
		DynamicDataSource.clearDataSource();
		return new BaseResult(flag,msg,record);
	}

	@Override
	public T selectByPrimaryKey(Object id) {
		if(null==id){
			id=-1;
		}
		DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
		T record=baseMapper.selectByPrimaryKey(Long.valueOf(id.toString()));
		DynamicDataSource.clearDataSource();
		return record;

	}

	@Override
	public List<T> findByAttr(String strWhere, String strOrder) {
		DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
        if(null==strOrder){
            strOrder=" gmt_modified DESC";
        }
		List<T>records=baseMapper.findByAttr(strWhere, strOrder);
		DynamicDataSource.clearDataSource();
		return records;
	}

	@Override
	public List<T> findByPage(Integer pageIndex, Integer pageSize,
			String strWhere, String strOrder) {
		DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
		if(null==strOrder){
			strOrder=" gmt_modified DESC";
		}
		List<T>records=baseMapper.findByPage(pageIndex, pageSize, strWhere, strOrder);
		DynamicDataSource.clearDataSource();
		return records;
	}

	@Override
	public int count(String strWhere) {
		DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
		int count=baseMapper.count(strWhere);
		DynamicDataSource.clearDataSource();
		return count;
	}


	@Override
	public int logicDeletion(Byte isDeleted, String strWhere) {
		DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
		int i=baseMapper.logicDeletion(isDeleted,strWhere);
		DynamicDataSource.clearDataSource();
		return i;
	}
}
